package com.A01.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.A01.sys.entity.EmployeeEntity;
import com.A01.sys.entity.EvaluateEntity;
import com.A01.sys.entity.UserEntity;
import com.A01.sys.service.EmployeeService;
import com.A01.sys.service.EvaluateService;
import com.A01.utils.ContextHolderUtils;
import com.A01.utils.RRException;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.A01.utils.PageUtils;
import com.A01.utils.R;



/**
 * 主观评价
 */
@RestController
@RequestMapping("dev/evaluate")
public class EvaluateController {
    @Autowired
    private EvaluateService evaluateService;
    @Autowired
    private EmployeeService employeeService;

    /**
     * 页码方式显示列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
        PageUtils page = evaluateService.queryPage(params);
        return R.ok().put("page", page);
    }
    /**
     * 根据身份证号查询评价
     */
    @RequestMapping("/employeeEvaluateList/{idNumber}")
    public R employeeEvaluateList(@PathVariable("idNumber") String idNumber){
        List<EvaluateEntity> list = evaluateService.selectList(new EntityWrapper<EvaluateEntity>().eq("id_number", idNumber));
        return R.ok().put("list", list);
    }

    /**
     * 根据id查询这一条记录
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        EvaluateEntity evaluate = evaluateService.selectById(id);

        return R.ok().put("evaluate", evaluate);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody EvaluateEntity evaluate){
        EmployeeEntity employeeEntity = employeeService.selectById(evaluate.getEmployeeId());//根据员工的id查询记录
        if (employeeEntity == null) {//判断员工是不是存在
            throw new RRException("员工不存在");
        }
        if(evaluate.getDescribe()==null){
            throw new RRException("评价信息不能为空");
        }
        UserEntity user = ContextHolderUtils.getUser();
        evaluate.setCompanyId(user.getCompanyId());
        evaluate.setIdNumber(employeeEntity.getIdNumber());
        evaluate.setCreateDate(new Date());
        evaluateService.insert(evaluate);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody EvaluateEntity evaluate){
        EmployeeEntity employeeEntity = employeeService.selectById(evaluate.getEmployeeId());
        if (employeeEntity == null) {
            throw new RRException("员工不存在");
        }
        if(evaluate.getDescribe()==null){
            throw new RRException("评价信息不能为空");
        }
        UserEntity user = ContextHolderUtils.getUser();
        evaluate.setCompanyId(user.getCompanyId());
        evaluate.setIdNumber(employeeEntity.getIdNumber());
        evaluate.setCreateDate(new Date());
        evaluateService.updateAllColumnById(evaluate);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        evaluateService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

}
